Initial commit

James Peret 7 years ago
commit
a1a2aa5e16
3 changed files with 66 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 15 0
      package.json
  3. 50 0
      src/hubot-multi-adaptor.coffee

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
1
+node_modules/

+ 15 - 0
package.json

@@ -0,0 +1,15 @@
1
+{
2
+  "name": "hubot-multi-adapter",
3
+  "version": "0.1.0",
4
+  "description": "A Hubot adapter that can receive messages thru socket.io or telegram",
5
+  "main": "index.js",
6
+  "scripts": {
7
+    "test": "echo \"Error: no test specified\" && exit 1"
8
+  },
9
+  "author": "James Peret",
10
+  "license": "ISC",
11
+  "dependencies": {
12
+    "express": "^4.15.2",
13
+    "socket.io": "^2.0.1"
14
+  }
15
+}

+ 50 - 0
src/hubot-multi-adaptor.coffee

@@ -0,0 +1,50 @@
1
+{Adapter,TextMessage} = require 'hubot'
2
+
3
+port = parseInt process.env.HUBOT_SOCKETIO_PORT or 9090
4
+console.log("socket.io server on port " + port);
5
+
6
+io = require('socket.io').listen port
7
+
8
+express = require('express')
9
+app = express()
10
+
11
+class MultiAdapter extends Adapter
12
+
13
+  constructor: (@robot) ->
14
+    @sockets = {}
15
+    super @robot
16
+
17
+  send: (user, strings...) ->
18
+    socket = @sockets[user.room]
19
+    console.log("Sending response to user " + user.name + ":")
20
+    console.log(str for str in strings)
21
+    for str in strings
22
+      socket.emit 'message', str
23
+
24
+  reply: (user, strings...) ->
25
+    socket = @sockets[user.room]
26
+    for str in strings
27
+      socket.emit 'message', str
28
+
29
+  run: ->
30
+    io.sockets.on 'connection', (socket) =>
31
+      @sockets[socket.id] = socket
32
+      console.log("New user connect (" + socket.id + ")")
33
+      @robot.brain.set 'log_id_' + socket.id, new Date().getUTCMilliseconds();
34
+
35
+      socket.on 'message', (data) =>
36
+        user = @userForId socket.id, name: data.username, room: socket.id
37
+        console.log("Message Received from user " + data.username + ":" )
38
+        console.log(data.message)
39
+        user.name = data.username
40
+        @receive new TextMessage user, data.message
41
+
42
+      socket.on 'disconnect', =>
43
+        console.log("User disconected (" + socket.id + ")")
44
+        @robot.brain.remove 'log_id_' + socket.id
45
+        delete @sockets[socket.id]
46
+
47
+    @emit 'connected'
48
+
49
+exports.use = (robot) ->
50
+  new MultiAdapter robot